package com.gluck.cases.common.utils.utils;


import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.gluck.cases.common.utils.enums.BizSerErrorEnum;
import com.gluck.cases.common.utils.constants.AuthConstants;
import com.gluck.cases.common.utils.exception.BizSerException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @ClassName JwtUtil
 * @Desc jwt 工具类
 * @Author I am Gluck
 * @Date 2022/5/17 - 11:49 上午
 **/
@Component
public class JwtUtil {

    /**
     * 过期时间改为从配置文件获取
     */
    private static String accessTokenExpireTime;

    /**
     * JWT认证加密私钥(Base64加密)
     */
    private static String encryptJWTKey;

    @Value("${accessTokenExpireTime}")
    public void setAccessTokenExpireTime(String accessTokenExpireTime) {
        JwtUtil.accessTokenExpireTime = accessTokenExpireTime;
    }

    @Value("${encryptJWTKey}")
    public void setEncryptJWTKey(String encryptJWTKey) {
        JwtUtil.encryptJWTKey = encryptJWTKey;
    }

    /**
     * 功能描述: 校验token
     * @Params [token]
     * @Return boolean
     * @Author I`m Gluck
     * @Date 2022/5/17
     */
    public static boolean verify(String token){
        try{
            String secret = getClaim(token, AuthConstants.USERNAME) + Base64ConvertUtil.decode(encryptJWTKey);
            Algorithm algorithm = Algorithm.HMAC256(secret);
            JWTVerifier build = JWT.require(algorithm).build();
            build.verify(token);
            return true;
        }catch (Exception e){
            throw new BizSerException(BizSerErrorEnum.VERIFY_TOKEN_ERROR, e.getMessage());
        }
    }

    /**
     * 功能描述: 获得Token中的信息无需secret解密也能获得
     * @Params [token, clain]
     * @Return java.lang.String
     * @Author I`m Gluck
     * @Date 2022/5/17
     */
    public static String getClaim(String token, String clain){
        try{
            DecodedJWT jwt = JWT.decode(token);
            return jwt.getClaim(clain).asString();
        }catch (Exception e){
            throw new BizSerException(BizSerErrorEnum.GET_CLAIM_ERROR, e.getMessage());
        }
    }


    /**
     * 功能描述: 生成签名
     * @Params [username, currentTimeMillis]
     * @Return java.lang.String
     * @Author I`m Gluck
     * @Date 2022/5/17
     */
    public static String sign(String username, String currentTimeMillis){
        try{
            String secret = username + Base64ConvertUtil.decode(encryptJWTKey);
            Date date = new Date(System.currentTimeMillis() + Long.parseLong(accessTokenExpireTime) * 1000);
            Algorithm algorithm = Algorithm.HMAC256(secret);
            String token = JWT.create()
                    .withClaim(AuthConstants.USERNAME, username)
                    .withClaim(AuthConstants.CURRENT_TIME_MILLIS, currentTimeMillis)
                    .withExpiresAt(date)
                    .sign(algorithm);
            return token;
        }catch (Exception e){
            throw new BizSerException(BizSerErrorEnum.BUILD_SIGN_ERROR, e.getMessage());
        }
    }
}
